package pw.hais.main.main_app.adapter;

import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.im.v2.AVIMMessage;
import com.avos.avoscloud.im.v2.messages.AVIMTextMessage;

import java.util.List;

import pw.hais.app.AppAdapter;
import pw.hais.http.Http;
import pw.hais.main.R;
import pw.hais.main.main_app.app.HaisApplication;
import pw.hais.utils.TimeUtil;

/**
 * Created by Hais1992 on 2015/12/14.
 */
public class ChatRoomMessageAdapter extends AppAdapter<AVIMMessage,ChatRoomMessageAdapter.ViewHolder> {


    public ChatRoomMessageAdapter(List<AVIMMessage> mList, Class<ViewHolder> mHolderClass) {
        super(mList, R.layout.activity_chatroom_list_item, mHolderClass);
    }

    @Override
    public void onBindView(int position, ViewHolder mViewHolder, AVIMMessage mItem) {
        AVIMTextMessage textMessage = (AVIMTextMessage)mItem;

        mViewHolder.text_author.setText(mItem.getFrom()+" "+ TimeUtil.getStrTime(mItem.getTimestamp()/1000 +""));
        mViewHolder.text_message.setText(textMessage.getText());

        if(mItem.getFrom().equals(HaisApplication.app.user.username)){
            if(textMessage.getAttrs()!=null){
                Http.displayImage(mViewHolder.image_photo_right,textMessage.getAttrs().get("userPhoto")+"");
            }else{
                mViewHolder.image_photo_right.setImageResource(R.drawable.ic_launcher);
            }
            mViewHolder.image_photo_left.setVisibility(View.GONE);
            mViewHolder.image_photo_right.setVisibility(View.VISIBLE);
            mViewHolder.text_author.setGravity(Gravity.RIGHT);
            mViewHolder.text_message.setGravity(Gravity.RIGHT);
        }else{
            if(textMessage.getAttrs()!=null){
                Http.displayImage(mViewHolder.image_photo_left, textMessage.getAttrs().get("userPhoto") + "");
            }else{
                mViewHolder.image_photo_left.setImageResource(R.drawable.ic_launcher);
            }
            mViewHolder.image_photo_right.setVisibility(View.GONE);
            mViewHolder.image_photo_left.setVisibility(View.VISIBLE);
            mViewHolder.text_author.setGravity(Gravity.LEFT);
            mViewHolder.text_message.setGravity(Gravity.LEFT);
        }
    }

    //反射免FindViewById，名称必须和 XML的 控件ID一样
    public static class ViewHolder extends AppAdapter.ViewHolder {
        public TextView text_author;
        public TextView text_message;
        public ImageView image_photo_left;
        public ImageView image_photo_right;

    }
}
